package com.fantabest.console.client.mvc;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.fantabest.common.client.AppEvents;
import com.fantabest.common.client.FantaBestConstants;
import com.fantabest.common.client.RegistryKeys;
import com.fantabest.console.client.widget.ConsoleMenu;
import com.fantabest.console.client.widget.ImportPlayersPanel;
import com.fantabest.console.client.widget.LoginPanel;
import com.fantabest.console.client.widget.NorthContainer;
import com.fantabest.console.client.widget.SeasonManagementPanel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * @author giant
 * 
 */
public class ConsoleView extends View {

	private Viewport viewPort;
	private NorthContainer north;
	private LayoutContainer west;
	private LayoutContainer center;
	private FitData centerData;
	private FantaBestConstants constants = Registry.get(RegistryKeys.CONSTANTS);
	
	public ConsoleView(Controller controller) {
		super(controller);
	}

	private void initUI() {
		viewPort = new Viewport();
		viewPort.setLayout(new BorderLayout());

		north = new NorthContainer();
		west = new LayoutContainer(new FitLayout());
		west.setVisible(false);

		center = new LayoutContainer(new CenterLayout());
		centerData = new FitData(5);

		Registry.register(RegistryKeys.NORTH_CONTAINER, north);
		Registry.register(RegistryKeys.WEST_CONTAINER, west);
		Registry.register(RegistryKeys.CENTER_PANEL, center);
		Registry.register(RegistryKeys.VIEWPORT, viewPort);

		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH,
				100);
		northData.setCollapsible(false);
		northData.setFloatable(false);
		northData.setHideCollapseTool(true);
		northData.setSplit(false);
		northData.setMargins(new Margins(0, 0, 0, 0));

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 0, 5, 0));

		viewPort.add(north, northData);
		// viewPort.add(west, westData);

		viewPort.add(center, centerData);

		RootPanel.get().add(viewPort);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType() == AppEvents.Init) {
			initUI();
		} else if (event.getType() == AppEvents.Login) {
			handleLogin(event);
		} else if (event.getType() == AppEvents.Logoff) {
			// Registry.unregister(RegistryKeys.MANAGER_LOGGED_IN);
			// north.getLoginPanel().logManagerOff();
		} else if (event.getType() == AppEvents.GoToLogin) {
			handleGoToLogin(event);
		} else if (event.getType() == AppEvents.MenuImportPlayers) {
			handleMenuImportPlayers(event);
		} else if (event.getType() == AppEvents.MenuManageSeason) {
			handleMenuManageSeason(event);
		} else if (event.getType() == AppEvents.Error) {
			handleError(event);
		}
	}

	private void handleLogin(AppEvent event) {
		if (GWT.getHostPageBaseURL().contains("localhost"))
			initUI();
		BeanModel managerBean = event.getData();
		if (managerBean != null) {
			Viewport viewport = Registry.get(RegistryKeys.VIEWPORT);
			Registry.register(RegistryKeys.MANAGER_LOGGED_IN, managerBean);
			LayoutContainer west = Registry.get(RegistryKeys.WEST_CONTAINER);

			BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST,
					241);
			westData.setSplit(true);
			westData.setCollapsible(true);
			westData.setMaxSize(241);
			westData.setMinSize(0);
			westData.setMargins(new Margins(10, 10, 10, 10));

			west.add(new ConsoleMenu());
			viewport.add(west, westData);

			LayoutContainer center = Registry.get(RegistryKeys.CENTER_PANEL);
			center.removeAll();

			north.getLoginTopBar().logManagerIn();

			viewport.layout(true);
		} else {
			MessageBox.alert("Errore", "Login fallito", null);
		}
	}

	private void handleGoToLogin(AppEvent event) {
		Viewport viewport = Registry.get(RegistryKeys.VIEWPORT);
		center.add(new LoginPanel());
		viewport.layout(true);
	}

	private void handleMenuImportPlayers(AppEvent event) {
		Viewport viewport = Registry.get(RegistryKeys.VIEWPORT);
		LayoutContainer center = Registry.get(RegistryKeys.CENTER_PANEL);
		center.removeAll();
		center.setLayout(new FitLayout());
		
		center.add(new ImportPlayersPanel());
		viewport.layout(true);
	}
	
	private void handleMenuManageSeason(AppEvent event) {
		Viewport viewport = Registry.get(RegistryKeys.VIEWPORT);
		LayoutContainer center = Registry.get(RegistryKeys.CENTER_PANEL);
		center.removeAll();
		center.setLayout(new FitLayout());
		center.add(new SeasonManagementPanel());
		viewport.layout(true);
	}
	
	private void handleError(AppEvent event)	{
		Throwable caught = event.getData();
		MessageBox.alert(constants.warning(), caught.getMessage(), null);
	}

}
